package com.zxy.ziems.server.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.server.monitor.convertor.MonitorConvertor;
import com.zxy.ziems.server.monitor.dao.MonitorGroupMapper;
import com.zxy.ziems.server.monitor.pojo.dto.MonitorGroupAddDTO;
import com.zxy.ziems.server.monitor.pojo.entity.MonitorGroupEntity;
import com.zxy.ziems.server.monitor.pojo.vo.MonitorGroupDeviceVO;
import com.zxy.ziems.server.monitor.pojo.vo.MonitorGroupVO;
import com.zxy.ziems.server.monitor.pojo.vo.MonitorTreeVO;
import com.zxy.ziems.server.monitor.service.MonitorDeviceService;
import com.zxy.ziems.server.monitor.service.MonitorGroupDeviceService;
import com.zxy.ziems.server.monitor.service.MonitorGroupService;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.utils.AssertUtil;
import com.zxy.ziems.server.utils.tree.TreeUtils;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Copyright (C),
 * @ClassName: MonitorGroupServiceImpl
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/5/10 14:34
 */
@Service
public class MonitorGroupServiceImpl extends ServiceImplX<MonitorGroupMapper, MonitorGroupEntity>
        implements MonitorGroupService {

    @Autowired
    private MonitorDeviceService monitorDeviceService;
    @Autowired
    private MonitorGroupDeviceService monitorGroupDeviceService;


    /**
     * 分组列表
     *
     * @return
     */
    @Override
    public List<MonitorGroupVO> getGroupList() {
        List<MonitorGroupEntity> list = this.list();

        List<MonitorGroupVO> voList = MonitorConvertor.INSTANCE.monitorGroupDOListConvert2VOList(list);

        return voList;
    }


    /**
     * 添加组
     *
     * @return
     */
    @Override
    public boolean addGroup(MonitorGroupAddDTO monitorGroupAddDTO) {
        // 检验名字是否存在

        QueryWrapper<MonitorGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MonitorGroupEntity::getMonitorGroupName, monitorGroupAddDTO.getMonitorGroupName());
        MonitorGroupEntity monitorGroupEntity = this.getOne(queryWrapper);
        AssertUtil.isFalse(null != monitorGroupEntity && !Objects.equals(monitorGroupAddDTO.getMonitorGroupName(), monitorGroupEntity.getMonitorGroupName()), "名字已存在");

        MonitorGroupEntity entity = MonitorConvertor.INSTANCE.monitorGroupDTOConvert2DO(monitorGroupAddDTO);


        return this.saveOrUpdate(entity);
    }

    @Override
    public boolean updateGroup(MonitorGroupAddDTO monitorGroupAddDTO) {
        // 检验名字是否存在
        if (monitorGroupAddDTO.getMonitorGroupName() != null) {
            QueryWrapper<MonitorGroupEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(MonitorGroupEntity::getMonitorGroupName, monitorGroupAddDTO.getMonitorGroupName());
            MonitorGroupEntity monitorGroupEntity = this.getOne(queryWrapper);
            AssertUtil.isFalse(null != monitorGroupEntity && !Objects.equals(monitorGroupAddDTO.getMonitorGroupName(), monitorGroupEntity.getMonitorGroupName()), "名字已存在");
        }

        MonitorGroupEntity entity = MonitorConvertor.INSTANCE.monitorGroupDTOConvert2DO(monitorGroupAddDTO);

        return this.saveOrUpdate(entity);
    }

    /**
     * 删除组
     *
     * @return
     */
    @Override
    @Synchronized
    public boolean deleteGroup(String id) {
        // 删除组关系
        monitorGroupDeviceService.removeByGroupId(id);
        return this.removeById(id);
    }

    @Override
    public List<MonitorTreeVO> devideTree() {
        List<MonitorGroupEntity> typeList = this.list();
        List<MonitorTreeVO> list = new ArrayList<>(
                typeList.stream().map((type) -> MonitorTreeVO.builder()
                                .id(type.getId())
                                .name(type.getMonitorGroupName())
                                .type(MonitorTreeVO.GROUP_TYPE)
                                .build())
                        .toList()
        );

        List<MonitorGroupDeviceVO> monitorGroupDeviceVOList = monitorGroupDeviceService.getDeviceAll();
        list.addAll(
                monitorGroupDeviceVOList.stream()
                        .map((device) -> MonitorTreeVO.builder()
                                .id(device.getId())
                                .deviceId(device.getDeviceId())
                                .parentId(device.getGroupId())
                                .name(device.getDeviceName())
                                .type(MonitorTreeVO.DEVICE)
                                .build()
                        ).toList()
        );

        return TreeUtils.tree(list);
    }


}
